package hu.codelab.lg.servicecenter;

import java.io.InputStream;

import hu.codelab.lg.servicecenter.entities.ServiceCenter;
import hu.codelab.lg.servicecenter.entities.ServiceCenters;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.preference.PreferenceManager;
import android.widget.Toast;

public final class ServiceCenterApplication extends Application {

    private ServiceCenters offlineServiceCenters;
    private ServiceCenter currentServiceCenter;

    @Override
    public void onCreate() {
        super.onCreate();

        final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        final long timeStamp = preferences.getLong(Constants.SP_REFRESH_TIME_STAMP, -1);
        if (timeStamp > 0 && (System.currentTimeMillis() - timeStamp) / 1000 / 60 / 60 / 24 / 30 > 1.0) {
            Toast.makeText(getBaseContext(), "Frissítsen adatbázist!", Toast.LENGTH_SHORT).show();
        }
    }

    public ServiceCenters getOfflineServiceCenters() {
        if (offlineServiceCenters == null) {
            refreshServiceCenterList();
        }
        return offlineServiceCenters;
    }

    public void refreshServiceCenterList() {
        final Serializer serializer = new Persister();

        try {
            InputStream in = openFileInput("service_list.xml");
            offlineServiceCenters = serializer.read(ServiceCenters.class, in);
            //System.out.println("refreshed");

            final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            preferences.edit().putLong(Constants.SP_REFRESH_TIME_STAMP, System.currentTimeMillis()).commit();
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    public void setOfflineServiceCenters(final ServiceCenters offlineServiceCenters) {
        this.offlineServiceCenters = offlineServiceCenters;
    }

    public ServiceCenter getCurrentServiceCenter() {
        return currentServiceCenter;
    }

    public void setCurrentServiceCenter(final ServiceCenter currentServiceCenter) {
        this.currentServiceCenter = currentServiceCenter;
    }

    public static boolean isNetworkAvailable(final Context context) {
        final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        boolean mobileNetworkAvailable = true;
        boolean wifiNetworkAvailable = true;

        final NetworkInfo[] infos = connectivityManager.getAllNetworkInfo();
        for (int i = 0; i < infos.length; i++) {
            final NetworkInfo info = infos[i];
            if (info.getType() == ConnectivityManager.TYPE_MOBILE && !info.isConnected()) {
                mobileNetworkAvailable = false;
            }
            if (info.getType() == ConnectivityManager.TYPE_WIFI && !info.isConnected()) {
                wifiNetworkAvailable = false;
            }
        }

        return mobileNetworkAvailable || wifiNetworkAvailable;
    }

}
